package com.example.nep.controller;


import com.example.nep.base.R;
import com.example.nep.dto.AdminLoginDTO;
import com.example.nep.model.Admin;
import com.example.nep.service.IAdminService;
import com.example.nep.util.JwtUtil;
import com.example.nep.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author han
 * @since 2024-04-12
 */
@CrossOrigin
@RestController
@RequestMapping("admin")
public class AdminController {
    @Resource
    private IAdminService adminService;
    @PostMapping("login")
    public R login(@RequestBody HashMap<String,Object> hashMap){
        String username = (String) hashMap.get("username");
        String password = (String) hashMap.get("password");
        AdminLoginDTO loginDTO = new AdminLoginDTO();
        loginDTO.setAdminCode(username);
        loginDTO.setPassword(password);

        Admin admin=adminService.login(loginDTO);
        if (admin==null){
            return R.error();
        }
        UserVO userVO=new UserVO();
        userVO.setAdminId(admin.getAdminId()+"");
        userVO.setRole(1);
        String token = JwtUtil.newToken(userVO);
        admin.setPassword("");
        return R.ok().setResult("user",admin).setResult("token",token);
    }
}
